/*
 *  Preferences.c
 *  xGPS
 *
 *  Created by Miguel Arroz on 10/06/29.
 *  Copyright Miguel Arroz. All rights reserved.
 *
 */

#include "Preferences.h"

#define DEFAULT_HOST "192.168.2.2"
#define DEFAULT_PORT 38714

xGPSPreferences preferences;

FILE *get_prefs_file(char *mode)
{
    char path[512];
    XPLMGetSystemPath(path);
    strcat(path, "Resources");
    strcat(path, XPLMGetDirectorySeparator());
    strcat(path, "preferences");
    strcat(path, XPLMGetDirectorySeparator());
    strcat(path, "xGPS.prf");
#if APL
    MacToUnixPath(path, path, 512);
#endif
	
	xGPSDebug( path );
    
    return fopen(path, mode);
}

char* getXGPSHost( void ) {
	return preferences.host;
}

unsigned short getXGPSPort( void ) {
	return preferences.port;
}

void setXGPSHost( char* host ) {
	char *copy = malloc( sizeof(char) * (strlen(host) + 1) );
	strlcpy( copy, host, strlen(host) + 1 );
	free( preferences.host );
	preferences.host = copy;
}

void setXGPSPort( unsigned short port ) {
	preferences.port = port;
}

void initPreferences( void ) {
	setXGPSHost( DEFAULT_HOST );
	setXGPSPort( DEFAULT_PORT );
	loadPreferences();
	createSockAddr( preferences );
}

void loadPreferences()
{
    xGPSDebug("Loading preferences");
    
    FILE *f = get_prefs_file("r");
    if (!f) {
		xGPSDebug("Could not load preferences (get_prefs_file returned null).");
		return;
	}
    
    char buffer[256];
    while (fgets(buffer, 256, f))
    {
        // Skip blank line.
        if (strlen(buffer) <= 1) continue;
        
        // Split buffer into name and data by the equals sign.
        char *name_end = strstr(buffer, " = ");
        if (name_end == NULL) continue;
        
        name_end[0] = '\0';
        char *name = buffer;
        char *data = name_end + 3;
		int i;
		
		for( i = 0; data[i] != '\0'; i++ ) {
			if( data[i] == '\n' ) {
				data[i] = '\0';
				break;
			}
		}
		
        if( strcmp( "host", name ) == 0 ) {
			setXGPSHost( data );
		} else if( strcmp( "port", name ) == 0 ) {
			setXGPSPort( (unsigned short)atoi(data) );
		} else {
			xGPSDebug("Unknown preference");
		}
	}
	
	if(f) {
		fclose(f);
	}
}

void savePreferences()
{
    xGPSDebug("Saving preferences");
    
    FILE *f = get_prefs_file("w");
    if (!f) return;
    
    fprintf( f, "%s = %s\n", "host", preferences.host );
    fprintf( f, "%s = %hu\n", "port", preferences.port );
    
    fclose(f);
	
	createSockAddr( preferences );
}

#if APL
#include <Carbon/Carbon.h>
int MacToUnixPath(const char * inPath, char * outPath, int outPathMaxLen)
{
    CFStringRef inStr = CFStringCreateWithCString(kCFAllocatorDefault, inPath, kCFStringEncodingMacRoman);
    if (inStr == NULL) return -1;
    
    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, inStr, kCFURLHFSPathStyle,0);
    CFRelease(inStr);
    
    CFStringRef outStr = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
    CFRelease(url);
    
    int success = CFStringGetCString(outStr, outPath, outPathMaxLen, kCFURLPOSIXPathStyle);
    CFRelease(outStr);
    return (success ? 0 : -1);
}
#endif
